#ifndef _io_wrapper_h
#define _io_wrapper_h

#include "typedefine.h"

#include <stdio.h>
#include <time.h>
#include <string>

typedef	FILE* skyFile;

typedef enum _e_sky_fileseek
{
	FILE_Seek_Begin = 0,
	FILE_Seek_Current,
	FILE_Seek_End
}e_sky_fileseek;

//#ifdef _WIN32


#ifdef __cplusplus
extern "C"
{
#endif
void	sky_assert(Int32 s, skyCStr str = 0, ...);

void	sky_trace_ui(skyCStr text, ...);
void	sky_assert_ui(Int32 s, skyCStr str = 0, ...);

void	sky_trace_ui_line(skyCStr text, ...);

void	sky_astrcpy(skyStr dest, skyCStr src);
void	sky_astrcpyn(skyStr dest, skyCStr src, Int32 n);
Int32	sky_astreql(skyCStr str1, skyCStr str2);
Int32	sky_astreqln(skyCStr str1, skyCStr str2, Int32 n);
Int32	sky_astrlen(skyCStr str);
void	sky_astrcat(skyStr dest, skyCStr src);
Int32	sky_astrfind(skyCStr str, skyCStr set);

void    sty_strcpy(skyStr dest, skyCStr src);

void	sky_ustrcpy(skyUStr dest, skyUCStr src);
void	sky_ustrcpyn(skyUStr dest, skyUCStr src, Int32 n);
Int32	sky_ustreql(skyUCStr str1, skyUCStr str2);
Int32	sky_ustreqln(skyUCStr str1, skyUCStr str2, Int32 n);
Int32	sky_ustrlen(skyUCStr str);
void	sky_ustrcat(skyUStr dest, skyUCStr src);
Int32	sky_ustrfind(skyUCStr str, skyUCStr set);

void	sky_fclose(skyFile fp);
Int32	sky_fread(Int8 *buffer, Int32 size, Int32 num, skyFile fp);
Int32	sky_fwrite(Int8 *buffer, Int32 size, Int32 num, skyFile fp);
Int32	sky_fseek(skyFile fp, Int32 offset, e_sky_fileseek origin);
Int32	sky_ftell(skyFile fp);

skyFile	sky_afopen(skyCStr filename, skyCStr option);
Int32	sky_afiledel(skyCStr filename);
skyFile	sky_ufopen(skyUCStr filename, skyCStr option);
Int32	sky_ufiledel(skyUCStr filename);
Int32   sky_mkdir(skyCStr dirname);
Int32   sky_rmdir(skyCStr dirname);

Int8* sky_getline(Int8* pStartOfLine);
Int8* sky_ftoa(double dSrc, Int8* aDst, Int32 aLen);

void *sky_malloc(Int32 size);
void sky_release(void *buf);
void *sky_memcpy(void *dest, const void *src, Int32 count);
void *sky_memset(void *dest, Int32 c, Int32 count);

void sky_a2ustrcpy(skyUStr udest, skyCStr asrc);
void sky_u2astrcpy(skyStr adest, skyUCStr usrc);

Int32 sky_getcpuusage();
Int32 sky_getmemoccupy(LONGLONG& mem, LONGLONG& vmem);
Int32 sky_getiobytes(LONGLONG& read_bytes, LONGLONG& write_bytes);

void sky_pack_int32(Int8*& buf_ptr, Int32 nValue);
void sky_pack_int16(Int8*& buf_ptr, Int16 nValue);
void sky_pack_str(Int8*& buf_ptr, const Int8* str);


#ifdef __cplusplus
}
#endif

//#endif

skyStr sky_utf8tombs(skyStr mbs, skyCStr utf8);
skyStr sky_mbstoutf8(skyStr utf8, const wchar_t* unicode);
wchar_t* sky_utf8tombs(wchar_t* unicode, skyCStr utf8);
Int8 sky_urlencodePart(Int32 input);
Int32 sky_urldecodePart(Int8 input);
bool sky_urlisSafe(Int32 ch);
void sky_urlencode(const std::string& input, std::string& output);
void sky_urldecode(const std::string& input, std::string& output);
Int8 const* sky_dataHeader();

#ifdef WIN32
#include <time.h>
Int32 gettimeofday(struct timeval*, Int32*);
#else
#include <stdarg.h>
#include <time.h>
#include <fcntl.h>
#endif


extern void		sky_strcpy(skyStr dest, skyCStr src);
extern void		sky_strcpy(skyUStr dest, skyUCStr src);
extern void		sky_strcpyn(skyStr dest, skyCStr src, Int32 n);
extern void		sky_strcpyn(skyUStr dest, skyUCStr src, Int32 n);
extern Int32	sky_streql(skyCStr str1, skyCStr str2);
extern Int32	sky_streql(skyUCStr str1, skyUCStr str2);
extern Int32	sky_streqln(skyCStr str1, skyCStr str2, Int32 n);
extern Int32	sky_streqln(skyUCStr str1, skyUCStr str2, Int32 n);
extern Int32	sky_strlen(skyCStr str);
extern Int32	sky_strlen(skyUCStr str);
extern void		sky_strcat(skyStr dest, skyCStr src);
extern void		sky_strcat(skyUStr dest, skyUCStr src);
extern Int32	sky_strfind(skyCStr str, skyCStr set);
extern Int32	sky_strfind(skyUCStr str, skyUCStr set);

extern skyFile	sky_fopen(skyCStr filename, skyCStr option);
extern skyFile	sky_fopen(skyUCStr filename, skyCStr option);
extern Int32	sky_filedel(skyCStr filename);
extern Int32	sky_filedel(skyUCStr filename);

//output print info
#define sky_Trace		sky_trace
#define sky_Assert		sky_assert
//#define sky_Trace		sky_trace_ui
//#define sky_Assert		sky_assert_ui
#define sky_Trace_Line	//sky_trace_ui_line

#define sky_Strcpy		sky_strcpy
#define sky_Strcpyn		sky_strcpyn
#define sky_Streql		sky_streql
#define sky_Streqln		sky_streqln
#define sky_Strlen		sky_strlen
#define sky_Strcat		sky_strcat
#define sky_Strfind		sky_strfind

#define sky_A2UStrcpy	sky_a2ustrcpy
#define sky_U2AStrcpy	sky_u2astrcpy

#define sky_Sprintf		sprintf
#define sky_Sscanf		sscanf

#define sky_FileOpen	sky_fopen
#define sky_FileClose	sky_fclose
#define sky_FileRead	sky_fread
#define sky_FileWrite	sky_fwrite
#define sky_FileSeek	sky_fseek
#define sky_FileTell	sky_ftell
#define sky_FileDel		sky_filedel
#define sky_MkDir       sky_mkdir
#define sky_RmDir       sky_rmdir

#define	sky_Malloc		sky_malloc
#define	sky_Free		sky_release
#define	sky_Memcpy		sky_memcpy
#define	sky_Memset		sky_memset

#define sky_MIN(a, b)		((a)<(b)? (a) : (b))
#define sky_MAX(a, b)		((a)>(b)? (a) : (b))
#define sky_RNDMINUS(a, b)	((a)>=(b)? ((a)-(b)) : ((a)-(b)+0x7fffffff))


#ifdef SKYNEW

/*#ifdef __cplusplus
class CSkyNew
{
public:
	CSkyNew(){}
	virtual ~CSkyNew(){}

	void *operator new(std::size_t size);

	void operator delete(void *p, std::size_t);
};
#endif*/
#endif

#endif
